﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;

namespace BOH.AccreditationSystem
{
    public partial class EvaluateApply : System.Web.UI.Page
    {
        BHUser user = null;
        private string UserID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDrop();
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    if (user.RightType == "U")
                    {
                        //DataSetApplyList(user.UnitID.Trim(), user.GovernID);
                        UnitDropBind(user.UnitID.Trim(), user.GovernID);
                    }
                    else if (user.RightType == "G")
                    {
                       // DataSetApplyList("-1", user.GovernID);
                        UnitGovernBind(user.GovernID);
                    }
                    else if (user.RightType == "C" || user.RightType == "A")
                    {
                        UnitGovernBind("");
                        //DataSetApplyList("-1", "-1");
                    }
                    UserID = user.UserID;

                }
                else
                {

                }

                if(Request.QueryString["type"]!=null&&Request.QueryString["ID"]!=null)
                {
                    if (Request.QueryString["type"].ToString() == "del")
                    {
                        delApply(Request.QueryString["ID"].ToString());
                    }
                    else if (Request.QueryString["type"].ToString() == "disabled")
                    {
                        LogicDelApply(Request.QueryString["ID"].ToString());
                    }
                }

               

            }
        }

        #region  <<  绑定申报信息  >>
        public DataSet dstApplyList(string EvaluationResult,string UnitID, string GovernID, string ApplyName, string IDCard, string ApplyPrintID, string ApplyType, string SpecialityID, string SubjectID, string JobRank, string ApplyPeriod)
        {
            DataSet dst = null;
            string UnitGovrenID = string.Empty;
            dst = ApplyClass.dstApplyEvaluation(EvaluationResult, UnitID, GovernID, ApplyName, IDCard, ApplyPrintID, ApplyType, SpecialityID, SubjectID, JobRank, (string)Session["ApplyPeriod"]);
            return dst;
        }

        public void DataSetApplyList(string UnitID, string GovernID)
        {
            DataSet dst = dstApplyList(drpStatusApply.SelectedValue,UnitID, GovernID, string.Empty, string.Empty, string.Empty, "-1", "-1", "-1", "-1", (string)Session["ApplyPeriod"]);
            BindApplyList(dst);
        }

        public void ApplyListDataSet()
        {
            DataSet dst = dstApplyList(drpStatusApply.SelectedValue,drpUnit.SelectedValue, drpGovern.SelectedValue, txtApplyName.Text.Trim(), txtIDCard.Text.Trim(), txtPrintID.Text.Trim(), drpApplyType.SelectedValue, drpSpeciality.SelectedValue, drpSubject.SelectedValue, drpApplyJobRank.SelectedValue, (string)Session["ApplyPeriod"]);
            BindApplyList(dst);
        }

        public void BindApplyList(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    spanTotal.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    labTotalNo.Text = AspNetPager1.RecordCount.ToString();
                    ApplyViewList.DataSource = pds;
                    ApplyViewList.DataBind();
                }
                else
                {
                    ApplyViewList.DataSource = null;
                    ApplyViewList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                    spanTotal.Visible = false;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                spanTotal.Visible = false;
                labNoRecord.Visible = true;
                ApplyViewList.DataSource = null;
                ApplyViewList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            ApplyListDataSet();
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            ApplyListDataSet();
        }

        protected void butDelete_Click(object sender, EventArgs e)
        {
            //if (hiddenSelected.Value.Trim().Length > 0 && hiddenSelected.Value.Trim() != ",")
            //{
            //    string[] arrApplyID = hiddenSelected.Value.Trim().Split(new char[] { ',' });
            //    foreach (string applyid in arrApplyID)
            //    {
            //        if (applyid.Trim().Length > 0)
            //        {
            //            ApplyClass.deleteApply(applyid);
            //        }
            //    }
            //    ApplyListDataSet();
            //}

        }

        #endregion

        #region <<  绑定查询条件选项  >>

        public void LoadDrop()
        {
            SubjectBind();
            BindCurrentJobRank();
            BindApplyType();
        }

        public void BindApplyType()
        {
            DataSet dst = OtherClass.dstgetApplyType();
            BindDropDownList(drpApplyType, dst);
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }

        public void BindCurrentJobRank()
        {
            drpApplyJobRank.Items.Clear();
            ListItem li1 = new ListItem("请选择职称", "-1");
            drpApplyJobRank.Items.Add(li1);
            DataSet dst = OtherClass.getJobRank();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString());
                drpApplyJobRank.Items.Add(li);
            }
        }

        #region <<  绑定申请学科组和专业  >>


        public void SubjectBind()
        {
            DataSet dst = Subject.getAllASubject();
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);

                }
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSubjectID = ((DropDownList)sender).SelectedValue;
            if (strSubjectID != "-1")
            {
                SpecialityBind(strSubjectID);
            }
            else
            {
                drpSpeciality.Items.Clear();
                ListItem li1 = new ListItem("请选择专业", "-1");
                this.drpSpeciality.Items.Add(li1);
            }
        }

        public void SpecialityBind(string SubjectID)
        {
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);
            DataSet dst = Subject.getAllSpecialityBySubjectID(SubjectID);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["SpecialityName"].ToString(), dr["SpecialityID"].ToString());
                this.drpSpeciality.Items.Add(li);
            }
        }

        #endregion

        #region <<  主管局和工作单位绑定  >>

        public void UnitGovernBind(string GovernID)
        {
            DataSet dstGovern = null;
            ListItem list = new ListItem("请选择主管局", "-1");
            drpGovern.Items.Add(list);
            dstGovern = UnitClass.getGovernByGovernID(GovernID);

            if (dstGovern.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dstGovern.Tables[0].Rows)
                {
                    if (row["GovernID"].ToString().Trim().Length != 0)
                    {
                        ListItem li = new ListItem(row["GovernName"].ToString(), row["GovernID"].ToString());
                        this.drpGovern.Items.Add(li);
                        //this.bindDropChildItem(row["GovernID"].ToString());
                    }
                }
            }
        }

        protected void drpGovern_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strGovernSelected = ((DropDownList)sender).SelectedValue;
            if (strGovernSelected != "-1")
            {
                string GovernID = strGovernSelected;
                bindDropChildItem(GovernID);
            }
            else
            {
                drpUnit.Items.Clear();
                ListItem list = new ListItem("请选择工作单位", "-1");
                drpUnit.Items.Add(list);
            }
        }

        public void bindDropChildItem(string GovernID)
        {
            drpUnit.Items.Clear();
            ListItem list = new ListItem("请选择工作单位", "-1");
            drpUnit.Items.Add(list);
            DataSet dstUnit = UnitClass.getUnitByGovern(GovernID);
            foreach (DataRow dr in dstUnit.Tables[0].Rows)
            {
                drpUnit.Items.Add(new ListItem(dr["UnitName"].ToString(), dr["UnitID"].ToString()));
            }
        }

        public void UnitDropBind(string UnitID, string GovernID)
        {
            string strUnitName = UnitClass.getUnitName(UnitID);
            ListItem li = new ListItem(strUnitName, UnitID);
            drpUnit.Items.Add(li);
            string strGovernName = UnitClass.GetGovernName(GovernID);
            ListItem liGovern = new ListItem(strUnitName, UnitID);
            drpGovern.Items.Add(liGovern);
        }

        #endregion

        #endregion

        public string showResult(string resutl)
        {
            string returnValue = string.Empty;
            returnValue = OtherClass.getDictDisplayByValue(resutl, "ApplyEvaluation");
            return returnValue;
        }

        protected void butBuildNo_Click(object sender, EventArgs e)
        {
               bool EvaluateResult = ApplyClass.BuildNumber((string)Session["ApplyPeriod"]);
               if (EvaluateResult)
               {
                   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('全部生成编号成功! ');</script>");
                   butSearch_Click(null, null);
               }
               else
               {
                   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('全部生成编号失败! ');</script>");
               }
               
        }

        protected void butbuilNoSubject_Click(object sender, EventArgs e)
        {
            string strSubjectID = this.drpSubject.SelectedValue.ToString();
            if (strSubjectID == "-1")
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请选择学科组! ');</script>");
            }
            else
            {
                bool EvaluateResult = ApplyClass.buildNumberSubject((string)Session["ApplyPeriod"], strSubjectID);
                if (EvaluateResult)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('学科组生成编号成功! ');</script>");
                    butSearch_Click(null, null);
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('学科组生成编号失败! ');</script>");
                }
            }
        }

        protected void butCleanNoSubject_Click(object sender, EventArgs e)
        {
            string strSubjectID = this.drpSubject.SelectedValue.ToString();
            if(strSubjectID=="-1")
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请选择学科组! ');</script>");
            }
            else
            {
                bool EvaluateResult = ApplyClass.CleanBuildNumber((string)Session["ApplyPeriod"], strSubjectID);
                if (EvaluateResult)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('清除学科组编号成功! ');</script>");
                    butSearch_Click(null, null);
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('清除学科组编号失败! ');</script>");
                }
            }
        }

        #region << 物理删除  >>
        public void delApply(string ApplyID)
        {
            int intResult = ApplyClass.deleteApply(ApplyID);
            if (intResult == 1)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertDel", "<script>alert('物理删除成功! ');</script>");
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertDel", "<script>alert('物理删除不成功! ');</script>");
            }
            butSearch_Click(null, null);
        }
        #endregion

        #region <<  逻辑删除  >>
        public void LogicDelApply(string ApplyID)
        {
            int intResult = ApplyClass.UpdateApplyUnActived(ApplyID);
            if (intResult == 1)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertlogicDel", "<script>alert('逻辑删除成功! ');</script>");
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertlogicDel", "<script>alert('逻辑删除不成功! ');</script>");
            }
            butSearch_Click(null, null);
        }
        #endregion
    }
}
